﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;
using System.Web.UI.WebControls;
using System.Data;
using DevExpress.Web.Data;
using System.Text;

/// <summary>
///ItemTemplate 的摘要说明
/// </summary>
public class ItemTemplate : ITemplate
{

    string columnName = "／";
    public ItemTemplate(string colname)
    {
        columnName = colname;
    }

    public void InstantiateIn(Control container) //关键是这个方法
    {
        ASPxLabel l = new ASPxLabel();
        l.ID = "topicname";
        l.Text = columnName;
        l.CssClass = "item";
        l.DataBinding += new EventHandler(this.OnDataBinding);
        l.Init += new EventHandler(Control_Init);
        container.Controls.Add(l);
    }
    //
    public void OnDataBinding(object sender, EventArgs e)
    {
        ASPxLabel l = (ASPxLabel)sender;
        GridViewDataItemTemplateContainer container = (GridViewDataItemTemplateContainer)l.NamingContainer; //update TAG
        l.Text = ((WebDataRow)container.DataItem)[columnName].ToString();
        //l.Text = container.Text.Replace("&nbsp;","");// ((WebDataRow)container.DataItem)[columnName].ToString();
       
    }

    void Control_Init(object sender, EventArgs e)
    {
        ASPxLabel l = (ASPxLabel)sender;
        GridViewDataItemTemplateContainer templateContainer = (GridViewDataItemTemplateContainer)l.NamingContainer;

        StringBuilder retContext = new StringBuilder();
        switch (templateContainer.Column.FieldName)
        {
            case "CiceroniID":
            case "CiceroniName":
                break;
            default:
                if (templateContainer.Grid.GetRowValues(templateContainer.VisibleIndex, templateContainer.Column.FieldName) == DBNull.Value)
                    return;
                DataTable tmpdt = GuideArrangeCtrl.GetGuideArrangeData(templateContainer.Column.FieldName, templateContainer.Grid.GetRowValues(templateContainer.VisibleIndex, "CiceroniID").ToString());
                if (tmpdt.Rows.Count == 0)
                    return;
                else
                {
                    for (int i = 0; i < tmpdt.Rows.Count; i++)
                    {
                        int hasEvent = int.Parse(tmpdt.Rows[i]["HasEvent"].ToString());

                        retContext.AppendFormat("导游:{0}<br>", tmpdt.Rows[i]["CiceroniName"].ToString());
                        if (hasEvent == 1 || hasEvent == 5)//是事假或紧急事件
                        {
                            retContext.AppendFormat("计划号:{0}<br>", tmpdt.Rows[i]["TourgroupID"].ToString());
                            retContext.AppendFormat("是否通知:{0}<br>", bool.Parse(tmpdt.Rows[i]["Notified"].ToString()) == true ? "是" : "否");
                        }
                        retContext.AppendFormat("起止日期:{0}至{1}<br>", tmpdt.Rows[i]["StartDate"].ToString(), tmpdt.Rows[i]["EndDate"].ToString());
                        retContext.AppendFormat("说明:{0}<br>", tmpdt.Rows[i]["Remark"].ToString());

                    }
                    l.Attributes.Add("value", retContext.ToString());
                    //l.ToolTip = retContext.ToString();
                    //l.Attributes.Add("value", tmpdt.Rows[0]["TourgroupID"].ToString());
                }

                break;
        }
    }
}
